public without sharing class ContactHandler {
    
	
	//计算联系人完整度 反写到客户上去
	public static void calcContactInfo(List<Contact> contacts ){

		Set<Id> accset = new Set<Id>();
		for (Contact  c :  contacts) {
			if(c.AccountId != null )accset.add(c.AccountId);
		}
		calc(accset);


	}

	///计算联系人完整度 反写到客户上去
	public static void calc(Set<Id> accset ){

		if(accset.isEmpty())return;
        List<Account>  accountList = new List<Account>();
        for(Account account :  [SELECT Id,RecordType.DeveloperName,
            (SELECT Id,CustomerCompleteness__c,DealerCompleteness__c FROM Contacts where  IncorporateCompleteness__c = true)
            FROM Account WHERE Id IN : accset   ] ){
            List<Contact> contacts = account.Contacts;
            Decimal completeness = 0;
                if(!contacts.isEmpty()){    
                    for(Contact contact : contacts){
                        if(contact.CustomerCompleteness__c!=null){
                            completeness += contact.CustomerCompleteness__c  ;
                        }
                    }
                    completeness /= contacts.size();
                }
                account.CustomerContactInfoIntegrity__c = completeness;

                accountList.add(account);
            }

 
        if(accountList.size() > 0)Database.update(accountList, false);

	}

}